package com.logistics.utils.tool;


import com.google.gson.GsonBuilder;

import java.lang.reflect.Field;
import java.util.*;

public class StringUtils {

	/**
	 * 判断对象是否为空
	 * @param
	 * @return
	 */
	public static boolean isNull(Object obj){
		if(obj instanceof String){
			return null == obj || "".equals(obj)?true:false;
		} else {
			return obj == null?true:false;
		}
	}
	
	/**
	 * 判断对象是否不为空
	 * @param object
	 * @return
	 */
	public static boolean isNotNull(Object object){
		return !isNull(object);
	}
	
	/**
	 * 字符串比较
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean isEquals(String str1, String str2){
		return str1 == null?false:str1.equals(str2)?true:false;
	}
	
	/**
	 * 字符串截取
	 * @param str
	 * @param
	 * @return
	 */
	public static String substring(String str, int sLength){
		return isNull(str)?null:str.substring(sLength);
	}
	
	/**
	 * 字符串截取
	 * @param str
	 * @param
	 * @return
	 */
	public static String substring(String str, int sLength, int eLength){
		return isNull(str)?null:str.substring(sLength,eLength);
	}
	
	/**
	 * 字符串截取返回数组
	 * @param str
	 * @param
	 * @return
	 */
	public static String[] split(String str, String regex){
		return isNull(str)?null:str.split(regex);
	}
	
	/**
	 * 字符串截取返回list
	 * @param str
	 * @param
	 * @return
	 */
	public static List<String> splitToList(String str, String regex){
		if(isNull(str)){
			return null;
		}
		List<String> resultList = new ArrayList<String>();
		List<Object> resultObject = arrayToList(str.split(regex));
		for(Object obj : resultObject){
			resultList.add(obj.toString());
		}
		return resultList;
	}
	
	/**
	 * 数组转list
	 * @param strArray
	 * @return
	 */
	public static List<Object> arrayToList(Object[] strArray){
		return isNull(strArray)?null:new ArrayList<Object>(Arrays.asList(strArray));
	}
	
	
	/**
	 * 把数组变成字符串分割
	 * @param arrays
	 * @param regex
	 * @return
	 */
	public static String join(Object[] arrays, String regex){
		if(isNull(arrays)){return null;}
		StringBuffer str = new StringBuffer();
		for(int i=0; i<arrays.length; i++){
			str = arrays.length-1==i?str.append(arrays[i]):str.append(arrays[i]+regex);
		}
		return str.toString();
	}
	
	/**
	 * 字符串连接
	 * @param regex
	 * @param str
	 * @return
	 */
	public static String concat(String regex, String... str){
		Object[] object = new Object[str.length];
		for(int i=0;i<str.length;i++){
			object[i] = str[i];
		}
		return join(object, regex);
	}
	
	/**
	 * 把string类型的json转换成bean
	 * @param json
	 * @param bean
	 * @return
	 */
//	public static <T> T stringToBena(String json, Class<T> bean){
//		return isNull(json)?null:(T) new GsonBuilder().create().fromJson(json, bean);
//	}

	/**
	 * 数组转string
	 * @return
     */
	@Deprecated
	public static String arrayToString(Object[] arrays, String regex){
		String str = "";
		for(int i=0;i<arrays.length;i++){
			str += concat(regex, arrays[i].toString());
		}
		return str;
	}

	/**
	 * java bean的copy
	 * @param obj1
	 * @param obj2
	 */
	public static void beanCopy(Object obj1, Object obj2){
		Class<?> cls1 = obj1.getClass();
		Class<?> cls2 = obj2.getClass();
		Field[] fields1 = cls1.getDeclaredFields();
		List<Field> fieldList = new ArrayList<Field>(Arrays.asList(fields1));
		if(StringUtils.isNotNull(cls1.getSuperclass())){
			Field[] fields = cls1.getSuperclass().getDeclaredFields();
			for(Field field : fields){
				fieldList.add(field);
			}
		}
		Field[] fields2 = cls2.getDeclaredFields();
		for(Field field1 : fieldList){
			field1.setAccessible(true);
			for(Field field2 : fields2){
				field2.setAccessible(true);
				if(StringUtils.isEquals(field1.getName(),field2.getName()) &&
						StringUtils.isEquals(field1.getType().getName(),field2.getType().getName())){
					try {
						field2.set(obj2, field1.get(obj1));
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * 把string类型的json转换成bean
	 * @param json
	 * @param bean
	 * @return
	 */
	public static <T> T stringToBena(String json, Class<T> bean){
		return isNull(json)?null:(T) new GsonBuilder().create().fromJson(json, bean);
	}

	/**
	 * bean 转 Map
	 * @param obj
	 * @return
     */
	public static Map<String,String> transBean2Map(Object obj) {
		Map<String,String> returnMap = new HashMap<String,String>();
		Class<?> cls = obj.getClass();
		Field[] fields = cls.getDeclaredFields();
		List<Field> fieldList = new ArrayList<Field>(Arrays.asList(fields));
		if(StringUtils.isNotNull(cls.getSuperclass())){
			Field[] fieldFathers = cls.getSuperclass().getDeclaredFields();
			for(Field field : fieldFathers){
				fieldList.add(field);
			}
		}
		for(Field field : fieldList){
			field.setAccessible(true);
			try {
				switch (field.getType().getName()){
					case "java.lang.String":
						if(StringUtils.isNotNull(field.get(obj))){
							returnMap.put(field.getName().toString(), field.get(obj).toString());
						}
						continue;
					default:
						continue;
				}
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return returnMap;
	}


	/**
	 * 集合转string
	 * @param stringList
	 * @param regex
     * @return
     */
	public static String listToString(List<String> stringList, String regex){
		if(isNull(stringList)){return null;}
		String result = null;
		Object[] object = new Object[stringList.size()];
		for (int i=0;i<stringList.size();i++) {
			object[i] = stringList.get(i);
		}
		return join(object,regex);
	}
}
